const sequelize = require('./database');
const User = require('../models/User');
const FavoriteLocation = require('../models/FavoriteLocation');

const initializeDatabase = async () => {
  try {
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 先删除所有表
    await sequelize.query('SET FOREIGN_KEY_CHECKS = 0');
    await sequelize.sync({ force: true });
    await sequelize.query('SET FOREIGN_KEY_CHECKS = 1');
    console.log('数据库表创建成功');

    // 创建测试用户
    const testUser = await User.create({
      username: 'test',
      email: 'test@example.com',
      password: 'password123'
    });
    console.log('测试用户创建成功:', testUser.id);

    // 创建测试收藏地点
    const testLocation = await FavoriteLocation.create({
      userId: testUser.id,
      location: 'Beijing',
      latitude: 39.9042,
      longitude: 116.4074
    });
    console.log('测试收藏地点创建成功:', testLocation.id);

    console.log('数据库初始化完成');
  } catch (error) {
    console.error('数据库初始化失败:', error);
  } finally {
    // 关闭数据库连接
    await sequelize.close();
  }
};

// 运行初始化
initializeDatabase(); 